package com.iot.gateway.aspectj.converter;

import com.iot.gateway.annotation.FieldConverter;

/**
 * 字符串转Double
 */
public class DoubleConverter implements FieldConverter {

    @Override
    public Object convert(Object value) {
        try {
            if (value instanceof String) {
                // 处理带千分位分隔符的数字（如 "1,234.56"）
                String cleaned = ((String) value).replaceAll("[^\\d.-]", "");
                return Double.parseDouble(cleaned);
            }
            return Double.parseDouble(value.toString());
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("无效的数值格式: " + value, e);
        }
    }

}
